package com.tencent.sr.iris.activity.service.event.handler;

import com.alibaba.fastjson.JSONObject;
import com.tencent.sr.iris.activity.common.util.TimeUtils;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityEventTypeEnum;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.interfaces.message.CommissionChangeEvent;
import com.tencent.sr.iris.activity.service.activity.IrisActivityService;
import com.tencent.sr.iris.activity.service.event.activity.IrisActivityExecutor;
import com.tencent.sr.rmall.common.exception.retail.TRetailBizException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Component;

/**
 * 佣金变更事件处理器
 *
 * 注：事件处理器的spring对象名使用对应事件枚举的code
 * com.tencent.sr.iris.activity.interfaces.enums.ActivityEventTypeEnum
 *
 * @author xiaojun
 */
@Slf4j
@Component("COMMISSION_CHANGE")
public class IrisCommissionChangeEventHandler extends AbstractIrisEventHandler {

    @Resource
    private IrisActivityService activityService;

    @Override
    public void after(ActivityEventDTO eventDTO) {

    }

    @Override
    void process(ActivityEventDTO eventDTO, List<String> eventHandingInfoList) {
        //补充事件pid
        addParameterForEvent(eventDTO);
        //查询佣金变更事件对应的活动
        List<ActivityDTO> activity = getActivity();
        activity.forEach(a -> {
            IrisActivityExecutor activityExecutor = getActivityExecutor(eventDTO, a);
            activityExecutor.execute(eventDTO, a);
            eventHandingInfoList
                    .add(this.getClass().getSimpleName() + "处理成功,idempotent:" + eventDTO.getIdempotent());
        });
    }

    @Override
    void paramPreProcessing(ActivityEventDTO msg) {
        try {
            if (ActivityEventTypeEnum.COMMISSION_CHANGE.getCode().equals(msg.getEventType())) {
                CommissionChangeEvent commissionChangeEvent = JSONObject
                        .parseObject(msg.getEventInfo(), CommissionChangeEvent.class);
                Date orderTime = new SimpleDateFormat(TimeUtils.YYYY_MM_DD).parse(commissionChangeEvent.getOrderTime());
                msg.setEventTime(orderTime.getTime());
            }
        } catch (Exception e) {
            log.error("事件时间解析失败,时间格式不正确,idempotent={},eventInfo={}", msg.getIdempotent(), msg.getEventInfo(), e);
            throw new TRetailBizException("事件时间解析失败,时间格式不正确");
        }
    }

    @Override
    List<ActivityTaskDTO> getRelatedTaskList(String eventType) {
        return null;
    }

    @Override
    void baseDataProcess(ActivityEventDTO eventDTO) {

    }

    protected List<ActivityDTO> getActivity() {
        List<ActivityDTO> activityDTOS = Lists.newArrayList();
        //查询月月领活动信息
        ActivityDTO monthActivity = activityService.getActivity(activityConfig.getMonthGiftActivityId());
        if (Objects.isNull(monthActivity)) {
            log.error("未查询到月月领活动信息,activityBizId={}", activityConfig.getMonthGiftActivityId());
        } else {
            activityDTOS.add(monthActivity);
        }
        //查询邀请有礼活动
        ActivityDTO inviteActivity = activityService.getActivityAllTask(activityConfig.getInviteGiftActivityId());
        if (Objects.isNull(inviteActivity)) {
            log.error("未查询到邀请有礼活动信息,activityBizId={}", activityConfig.getInviteGiftActivityId());
        } else {
            activityDTOS.add(inviteActivity);
        }
        //查询特邀邀请有礼活动
        ActivityDTO specialRegistActivity = activityService
                .getActivityAllTask(activityConfig.getSpecialInviteGiftActivityId());
        if (Objects.isNull(specialRegistActivity)) {
            log.error("未查询到特邀邀请有礼活动信息,activityBizId={}", activityConfig.getRegistGiftActivityId());
        } else {
            activityDTOS.add(specialRegistActivity);
        }
        return activityDTOS;
    }
}
